"""
Case Type   : 兼容M*数据库timestamp函数
Case Name   : timestamp类型和interval类型进行加减操作
Create At   : 2023.05.16
Owner       : z@wan005
Description :
    1、创建表
    2、插入数据
    3、timestamp类型和interval类型进行运算
    4、清理环境
Expect      :
    1、成功，默认带时区
    2、成功
    3、成功
    4、成功
History     :modified by z@wan005,2023.05.16，sql展示的结果不显示时区，修改用例为py
             modified by zhanghuan96,2024.6.20，修改预期，适配新代码
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest027(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_timestamp_0027"
        self.tb_name = "tb_timestamp_0027"

    def test_datatype(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:创建表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name};" \
                  f"create table {self.tb_name}(ts1 timestamp,ts2 interval);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败:' + text)

        text = '--step2:插入数据;expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name} " \
                  f"values('294277-1-9 4:00:54.2',interval 1 hour);" \
                  f"insert {self.tb_name} " \
                  f"values('1902-11-20 14:14:16.9999999',interval 2 minute);" \
                  f"insert {self.tb_name} " \
                  f"values('09-09-14 23:59:59.9999999',interval 3 second);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count('INSERT 0 1'), '执行失败:' + text)

        text = '--step3:timestamp类型和interval类型进行运算;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select ts1+ts2 from {self.tb_name};" \
                  f"select ts1-ts2 from {self.tb_name};" \
                  f"select timestamp'2001-09-08 01:00' + interval 23 hour;" \
                  f"select timestamp'2001-09-08 01:00' + interval 23 year;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        msg_lis = ['1902-11-20 14:16:17','2009-09-15 00:00:03',
                   '1902-11-20 14:12:17', '2009-09-14 23:59:57',
                   '2001-09-09 00:00:00', '2024-09-08 01:00:00']
        for msg in msg_lis:
            self.assertIn(msg, sql_res, '查询失败')

    def tearDown(self):
        text = '----step4:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                 dbname='postgres')
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
